<div class="easyui-panel" style="padding:10px;" data-options="fit:true,border:false">
    <div class="easyui-layout s_m_layout" data-options="fit:true">
        <div title="部门列表" data-options="region:'west',split:true,border:true,collapsible:false" style="width:220px; padding:20px;">
        	<ul id="user_tree"></ul>
        </div>
        <div data-options="region:'center',border:true">
            <table id="userlistdg"></table>
            <div id="userlisttoolbar" style="display:none;">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-add" plain="true" onclick="newUser()">新建</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-edit" plain="true" onclick="editUser()">编辑</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-remove" plain="true" onclick="destroyUser()">删除</a>
            </div>
            <div id="userlistdlg" class="easyui-dialog" data-options="modal:true,closed: true, buttons:'#userlist-dlg-buttons'" style="width:400px;height:400px;padding:10px 20px">
                <form id="userlistfm" method="post">
                	<table cellpadding="5">
                        <tr>
                            <td>用 户 名:</td>
                            <td><input name="user_name" id="user_name_readonly" class="easyui-textbox" required="true" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>真实姓名:</td>
                            <td><input name="user_actual" class="easyui-textbox" required="true" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>别　　名:</td>
                            <td><input name="user_aliases" class="easyui-textbox" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>用户密码:</td>
                            <td><input type="password" name="user_pass" class="easyui-textbox" required="true" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>用户角色:</td>
                            <td><input class="easyui-combobox" id="role_id" name="role_id" data-options="panelHeight:'auto',valueField:'id',textField:'text',required:true,editable:false" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>部　　门:</td>
                            <td><input id="dept_id" name="dept_id" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>排　　序:</td>
                            <td><input id="user_sort" name="user_sort" class="easyui-numberbox" data-options="min:1,max:100,required:true" missingMessage="输入排序号，1-100以内" style="width:200px;"></td>
                        </tr>
                        <tr>
                        <tr>
                            <td>状　　态:</td>
                            <td><input type="radio" id="user_disable_0" name="user_disable" value="0"><label for="user_disable_0">停用</label>
                            <input type="radio" id="user_disable_1" name="user_disable" value="1"><label for="user_disable_1">启用</label></td>
                        </tr>
                	</table>                    
                </form>
            </div>
            <div id="userlist-dlg-buttons">
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-ok" onclick="saveUser()">保存</a>
                <a href="javascript:void(0)" class="easyui-linkbutton" iconCls="icon-cancel" onclick="javascript:$('#userlistdlg').dialog('close')">取消</a>
            </div>
        </div>
    </div>
</div>
<script type="text/javascript">
	var url,isedit;
	function c_user_dept_tree(){
		$('#user_tree').tree({
			url:'<?php echo base_url('systemmanage/dept_data')?>',
			method:'post',
			animate:true,
			lines:false,
			fit:true,
			onSelect : function(node) {
				//返回树对象
				var tree = $(this).tree;
				//选中的节点是否为叶子节点,如果不是叶子节点,清除选中
				var isLeaf = tree('isLeaf', node.target);
				if (!isLeaf) {
					//清除选中
					$('#user_tree').tree('toggle',node.target);
					//$.messager.alert('警告','请选择子部门!','warning');
				}else{
					//$('#user_tree').tree({
					//	onClick: function(node){
							//alert(node.text);  // alert node text property when clicked
					set_userlist_tree(node);
							
					//	}
					//});	
				}
			}
		});
	}
	c_user_dept_tree();
	
	function set_userlist_tree(node){
		$('#userlistdg').datagrid({
			url:'<?php echo base_url('systemmanage/user_data');?>/'+node.id+'?_='+GetRandomNum(),
			singleSelect:true,
			method:'post',
			rownumbers:true,
			border:false,                
			toolbar:'#userlisttoolbar',
			title:node.text+' 用户列表',
			columns:[[
				{field:'user_name',title:'用户名',width:100,align:'center'},
				{field:'user_actual',title:'真实姓名',width:100,align:'center'},
				{field:'user_aliases',title:'别名',width:100,align:'center'},
				{field:'role_name',title:'角色',width:100,align:'center'},
				{field:'dept_name',title:'部门',width:150,align:'center'},
				{field:'user_sort',title:'排序',width:100,align:'center'},
				{field:'user_disable',title:'状态',width:100,align:'center',
					formatter: function(value,row,index){
						if(value == 1){
							return '正常';
						} else {
							return '停用';
						}
					},
					styler: function(value,row,index){
						if (value == 0){
							return 'background-color:#ffee00;color:red;';
						}
					}
				}
			]],
			onDblClickRow:function(index,data){
				editUser();
			}		
		});	
	}
	$('#dept_id').combotree( {//添加或编辑用户部门时只能选中叶子节点
		//获取数据URL
		url : '<?php echo base_url('systemmanage/dept_data')?>',
		panelWidth: 300,
		panelHeight:300,
		method:'post',
		required:true,
		//选择树节点触发事件
		onSelect : function(node) {
			//返回树对象
			var tree = $(this).tree;
			//选中的节点是否为叶子节点,如果不是叶子节点,清除选中
			var isLeaf = tree('isLeaf', node.target);
			if (!isLeaf) {
				//清除选中
				$('#userlistdlg').dialog('close');        // close the dialog
				$('#dept_id').combotree('clear');
				$.messager.alert('警告','请选择子部门!','warning');
			}
		}
	});

	function newUser(){
		isedit = false;
		$('#userlistdlg').dialog('open').dialog('setTitle','新建用户');
		$('#userlistfm').form('clear');
		$('#userlistfm').form('load',{'user_sort':'10','user_disable':'1'});
		$("#user_name_readonly").textbox('readonly',false);	// eanble readonly mode	
		url = '<?php echo base_url('systemmanage/user_add')?>';
		$('#role_id').combobox('reload', '<?php echo base_url('systemmanage/role_data_select');?>');
		$("#user_name_readonly").textbox('enable');	// disable readonly mode	
		var node = $('#user_tree').tree('getSelected');
		if (node){
			 $('#dept_id').combotree('setValue', node.id);
		}
	}
	function editUser(){
		isedit = true;	
		
		var row = $('#userlistdg').datagrid('getSelected');
		if (row){
			$('#userlistdlg').dialog('open').dialog('setTitle','编辑用户');
			$('#userlistfm').form('load',row);
			url = '<?php echo base_url('systemmanage/user_edit')?>/'+row.id;
			$('#role_id').combobox('reload', '<?php echo base_url('systemmanage/role_data_select');?>/'+row.role_id);
			$("#user_name_readonly").textbox('readonly',true);	// eanble readonly mode	
		}else{
			$.messager.alert('提示','编辑用户，请先指定用户!','warning');
		}
	}
	function saveUser(){
		$('#userlistfm').form('submit',{
			url: url,
			onSubmit: function(){
				//alert($(this).form('validate'));
				return $(this).form('enableValidation').form('validate');
			},
			success: function(result){
				var result = eval('('+result+')');
				if(result.success == true){
					var node = $('#user_tree').tree('getSelected');
					set_userlist_tree(node);
					if(isedit){
						$('#userlistfm').form('clear');			
						$('#userlistdlg').dialog('close');        // close the dialog
					}else{
						$.messager.confirm('成功','添加成功，继续添加吗?',function(r){
							if (r){
								$('#userlistfm').form('load',{'user_name':'','user_actual':'','user_aliases':'','user_pass':'','user_sort':'10','user_disable':'1'});
							}else{
								$('#userlistfm').form('clear');			
								$('#userlistdlg').dialog('close');        // close the dialog
								//$('#userlistdg').datagrid('reload');    // reload the user data
							}
						});
					}
				}else{
					$.messager.alert('警告',result.errorMsg,'warning');	
				}
			}
		});
	}
	function destroyUser(){
		var row = $('#userlistdg').datagrid('getSelected');
		if (row){
			if(row.user_name === 'admin'){
				$.messager.alert('错误','管理员账户禁止删除','error');
			}else{
				$.messager.confirm('确认','你确定要删除这个用户吗?',function(r){
					if (r){
						$.post('<?php echo base_url('systemmanage/user_delete');?>',{id:row.id},function(result){
							if (result.success){
								//$('#userlistdg').datagrid('reload');    // reload the user data
								var node;
								node = $('#user_tree').tree('getSelected');
								set_userlist_tree(node);
							} else {
								$.messager.alert('错误',result.errorMsg,'error');
							}
						},'json');
					}
				});
			}
		}else{
			$.messager.alert('提示','删除用户，请先指定用户!','warning');
		}
	}
 
</script>